package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.service.CateGoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author : [wangtao]
 * @className : DishController
 * @description : []
 * @createTime : [2022/10/10 17:45]
 * @updateTime : [2022/10/10 17:45]
 */
@RestController
@Slf4j
@RequestMapping("/dish")
public class DishController {
@Autowired
private DishService dishService;
@Autowired
private CateGoryService cateGoryService;
@Autowired
private DishFlavorService dishFlavorService;
/*
 * @author wangtao
 * @description  添加菜品功能
 * Function: add
 * @param [dish]
 * @return com.itheima.reggie.common.R<java.lang.String>
 * @createTime  2022/10/10 19:45
 */
@PostMapping
public R<String> add(@RequestBody DishDto dish){
//    log.info("DishDto:  {}",dish);
    return dishService.SaveDish(dish);
}

/*
 * @author wangtao
 * @description 分页展示功能
 * Function: page
 * @param [page, pageSize, name]
 * @return com.itheima.reggie.common.R
 * @createTime  2022/10/11 10:30
 */
@GetMapping("/page")
public R page(int page,int pageSize,String name){
    log.info("page:{},pageSize:{},name:{}",page,pageSize,name);
    return dishService.pageInfo(page,pageSize,name);
}
/*
 * @author wangtao
 * @description 菜品管理修改功能数据回显
 * Function: update
 * @param [id]
 * @return com.itheima.reggie.common.R<com.itheima.reggie.dto.DishDto>
 * @createTime  2022/10/11 18:35
 */
    @GetMapping("/{id}")
    public R<DishDto> update(@PathVariable Long id) {
    log.info("id= {},",id);
        Dish byId = dishService.getById(id);
//        Long categoryId = byId.getCategoryId();
        DishDto dishdto=new DishDto();
        BeanUtils.copyProperties(byId,dishdto);
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId, id);
//        Category category = cateGoryService.getById(categoryId);
//        String name = category.getName();
//       dishdto.setCategoryName(name);
        List<DishFlavor> list = dishFlavorService.list(lqw);
        dishdto.setFlavors(list);
//        log.info("dishdto======================{}",dishdto);
        return R.success(dishdto);
}
/*
 * @author wangtao
 * @description 菜品管理修改功能数据保存
 * Function: updatesave
 * @param []
 * @return com.itheima.reggie.common.R
 * @createTime  2022/10/11 18:35
 */
@PutMapping
public R<String> updatesave(@RequestBody DishDto dishdto) {
    log.info("dishDto:{}",dishdto);
    dishService.updatesave(dishdto);
    return R.success("菜品更新成功");
}

    @GetMapping("/list")
    public R<List<DishDto>> list(Dish dish) {
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(dish.getCategoryId() != null, Dish::getCategoryId, dish.getCategoryId()).eq(Dish::getStatus, 1).orderByDesc(Dish::getSort).orderByAsc(Dish::getCreateTime);
        List<Dish> listDish = dishService.list(lqw);
        List<Long> collect = listDish.stream().map(listd -> listd.getId()).collect(Collectors.toList());
        LambdaQueryWrapper<DishFlavor> df = new LambdaQueryWrapper<>();
        df.in(DishFlavor::getDishId, collect);
        List<DishFlavor> list = dishFlavorService.list(df);
        List<DishDto> dishDtos = new ArrayList<>();
        for (Dish listdish : listDish) {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(listdish, dishDto);
            List<DishFlavor> flavors = list.stream().filter(key -> key.getDishId().equals(dishDto.getId())).collect(Collectors.toList());
            dishDto.setFlavors(flavors);
            dishDtos.add(dishDto);
        }

        System.out.println(listDish);
        return R.success(dishDtos);
    }

}
